package com.shao.web;

import com.shao.pojo.Book;
import com.shao.pojo.Page;
import com.shao.service.BookService;
import com.shao.service.impl.BookServiceImpl;
import com.shao.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: SJP
 * Date: 2021/5/17
 * Time: 20:23
 * Description: No Description
 */
public class BookServlet extends BaseServlet {

    BookService bookService = new BookServiceImpl();

    /**
     * 展示所有图书
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过BookService查询所有图书
        List<Book> books = bookService.queryBooks();
        //将查询到的数据保存到request域中
        req.setAttribute("books", books);
        //请求转发到/pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
    }

    /**
     * 添加图书
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Book book = WebUtils.copyParaToBean(new Book(), req.getParameterMap());
        bookService.addBook(book);

        int i = 0;
        System.out.println(i++);
        //请求转发存在刷新之后二次请求,因为地址没有发生变化
        //req.getRequestDispatcher("/manager/bookServlet?action=page").forward(req,resp);
        //使用请求重定向，地址发生了变化
       resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNn="+req.getParameter("pageNn"));
    }

    /**
     * 删除图书
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数id'
        String id = req.getParameter("id");
        //调用Bookservlet实现删除
        bookService.deleteBookById(WebUtils.parseInt(id, 0));
        //请求重定向到图书管界面，并重新查询数据库
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNn="+req.getParameter("pageNn"));
    }

    /**
     * 获取图书信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数id
        String id = req.getParameter("id");
        //调用QueryforOne查询书籍信息
        Book book= bookService.queryBookById(WebUtils.parseInt(id,0));
        //将获取的信息保存到request域中
        req.setAttribute("book",book);
        //请求转发到图书修改页面
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }

    /**
     * 修改图书信息
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数
        Book book =WebUtils.copyParaToBean(new Book(),req.getParameterMap());
        //调用update
        bookService.updateBook(book);
        //重定向到图书管理
        resp.sendRedirect(req.getContextPath()+"/manager/bookServlet?action=page&pageNn="+req.getParameter("pageNn"));
    }

    /**
     * 处理分页功能
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求参数当前页码pageNn和页显示数据数pageSize
        int pageNn = WebUtils.parseInt(req.getParameter("pageNn"),1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"),Page.PAGE_SIZE);
        //2 调用BookService.page(pageNn，pageSize)：Page对象
        Page<Book> page = bookService.page(pageNn,pageSize);
        page.setUrl("manager/bookServlet?action=page");
        //将请求参数保存到request域中
        req.setAttribute("page",page);
        //请求转发到pages/manager/book_manage.jsp
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
}